package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domain.Dict_district;
import com.example.result.JsonResult;
import com.example.service.Dict_districtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/dict_district")
public class Dict_districtController extends BaseController{
    @Autowired
    private Dict_districtService service;
    //获取所有的省/直辖市
    @GetMapping("/getAllProvince")
    public JsonResult getAllProvince(){
        QueryWrapper<Dict_district> wrapper=new QueryWrapper<>();
        wrapper.eq("parent","86");   //省和直辖市parent都为86
        wrapper.orderBy(true,true,"id","parent");

        List<Dict_district> list = service.list(wrapper);
        if(list==null){
            throw new RuntimeException("数据有误");
        }
        return new JsonResult(true,"",list);
    }
    //获取符合条件的城市
    @GetMapping("/getListCity/{parent}")
    public JsonResult getListCity(@PathVariable String parent){
        System.out.println("获取到的城市parent"+parent);
        QueryWrapper<Dict_district> wrapper=new QueryWrapper<>();
        wrapper.eq("parent",parent);
        wrapper.orderBy(true,true,"id","parent");

        List<Dict_district> list = service.list(wrapper);
        if(list==null){
            throw new RuntimeException("数据有误");
        }
        return new JsonResult(true,"",list);
    }
    //获取符合条件的区/县
    @GetMapping("/getListArea/{parent}")
    public JsonResult getListArea(@PathVariable String parent){
        System.out.println("获取到的区/县parent"+parent);
        QueryWrapper<Dict_district> wrapper=new QueryWrapper<>();
        wrapper.eq("parent",parent);
        wrapper.orderBy(true,true,"id","parent");

        List<Dict_district> list = service.list(wrapper);
        if(list==null){
            throw new RuntimeException("数据有误");
        }
        return new JsonResult(true,"",list);
    }
}
